package com.joymart.admin.rest;


import com.joymart.common.model.Range;
import com.joymart.common.utils.DatetimeUtils;
import com.joymart.common.utils.SecurityUtils;
import com.joymart.manual.operation.log.model.ManualOperationLog;
import com.joymart.manual.operation.log.service.ManualOperationLogService;
import com.joymart.prepaid.wallet.service.PrepaidWalletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Map;

@RestController
@RequestMapping("/admin")
public class PrepaidWalletAdminController {

    @Autowired
    private PrepaidWalletService prepaidWalletService;

    @Autowired
    private ManualOperationLogService manualOperationLogService;

    /**
     * 预付钱包充值
     * @param userId
     * @param amount
     */
    @PutMapping(value = "/prepaidWallet/{userId}/balance" ,params = "action=INCREASE")
    public void increaseBalance(@PathVariable String userId , @RequestParam BigDecimal amount){
        SecurityUtils.requiresRestAuthority("OrderRelated");
        prepaidWalletService.increase(userId, amount);
        Map<String,Object> details = Map.of("action","INCREASE","amount",amount,"postBalance",
                prepaidWalletService.findByUserId(userId).getBalance());
        manualOperationLogService.insert("PREPAID_WALLET", userId, details , SecurityUtils.getCurrentUserId());
    }

    /**
     * 预付钱包扣款
     * @param userId
     * @param amount
     */
    @PutMapping(value = "/prepaidWallet/{userId}/balance",params = "action=DECREASE")
    public void decreaseBalance(@PathVariable String userId , @RequestParam BigDecimal amount){
        SecurityUtils.requiresRestAuthority("OrderRelated");
        prepaidWalletService.decrease(userId, amount);
        Map<String,Object> details = Map.of("action","DECREASE","amount",amount,"postBalance",
                prepaidWalletService.findByUserId(userId).getBalance());
        manualOperationLogService.insert("PREPAID_WALLET", userId, details , SecurityUtils.getCurrentUserId());
    }

    @GetMapping("/prepaidWallet/logs")
    public Page<ManualOperationLog> searchStockOperationLogs(
            @RequestParam(required = false) LocalDateTime start,
            @RequestParam(required = false) LocalDateTime end,
            @RequestParam(value = "pageNo", defaultValue = "0")Integer pageNo,
            @RequestParam(value = "pageSize", defaultValue = "20")Integer pageSize,
            @RequestParam(value = "operatorId", defaultValue = "") String operatorId,
            @RequestParam(value = "ownerId", defaultValue = "") String ownerId){


        if(start == null){
            start = DatetimeUtils.now().minusDays(7);
        }

        if(end == null){
            end = DatetimeUtils.now();
        }
        Range range = Range.of(start,end);
        Pageable pageable = PageRequest.of(pageNo,pageSize);
        return manualOperationLogService.search(pageable, range, "PREPAID_WALLET", ownerId, operatorId );
    }



}
